阿里云Dataq发布的api 调用的工具类

说明

dataq 上有调用的示例,但是实际,自己新建的调用不通原因有以下几点需要注意

  1. akId,akSecret 这个是用户下面生成的,建议新生成一个,用原来老的有时会调用不通
  2. 租户信息TenantCode,这个一定要注意,没告诉你在哪,实际上是用户下面的 Tenant Code,在用户id的上面,非常不显眼,注意要用自己的
  3. 示例中没有说明需要跳过ssl证书,调用的时候会告诉你证书不可信,下面工具类已经自动跳过证书
  4. 使用main 方法直接跑调用,会报错,401 ,实际上是接口有限流,很多时候,多调用几次,就成功了
  5. 建议加报错重新调用的方法

工具类DataqUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.example.utils;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DataqUtil {

//日志
protected final Log logger = LogFactory.getLog(getClass());

// ak信息
public final static String akId = "akId";//用户信息中生成
public final static String akSecret = "akSecret";// 用户信息中生成
// 租户信息
public final static String TenantCode= "TenantCode"; //租户信息需要在dataq 中查找tenant_一串数字

public final static String reqmethod = "POST";


/**
* 调用阿里dataQ
* @param url
* @param params
* @return
*/
public Map<String,Object> getDataQApi(String url,Map<String,String> params){
Map<String,Object> mapResult = new HashMap<String,Object>();

//参数
Map<String,Object> bodyMap = new HashMap<>();
bodyMap.put("params",params);
String body = JSON.toJSONString(bodyMap);
logger.trace("body---------------->"+body);

try {
// 生成签名,签名需要放在header里 重要 !!!!!
String signature = generateSignature(reqmethod, akId, akSecret, url, body);

// 创建http连接
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setUseCaches(false);
// 设置信息校验的头部,重要 !!!!
conn.setRequestMethod(reqmethod);
// 设置租户信息 必填
conn.setRequestProperty("X-Access-TenantCode", TenantCode);

// accept 必填
conn.setRequestProperty("Accept", "json");
// contentType 必填
conn.setRequestProperty("Content-type", "application/json");
// 日期 必填
conn.setRequestProperty("date", toGMTString(new Date()));
// 签名 必填
conn.setRequestProperty("Authorization", signature);
// body体 md5 post/put必填( get请求不用填 )
conn.setRequestProperty("Content-MD5", MD5Base64(body));
conn.setDoOutput(true);
conn.setDoInput(true);
// 获得返回结果
String result = getResult(body, reqmethod, conn);

mapResult.put("data",result);
mapResult.put("code",conn.getResponseCode());

mapResult.put("success", true);
mapResult.put("msg", "查询成功!");
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (ProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
mapResult.put("data","");
mapResult.put("code","500");
mapResult.put("success", false);
mapResult.put("msg", "查询失败!");
}
return mapResult;

}

public static void main(String[] args) {
DataqUtil dataqUtil = new DataqUtil();
String url = "https://analysis.dataq.res.sgmc.sgcc.com.cn/analysis/api/service/ads_grid_sbbqk_byqgzzd_mf_update?workspaceCode=dataQ_sd";
Map<String,String> params = new HashMap<String,String>();
params.put("page","0");
params.put("rows","1000");
Map<String, Object> dataq = dataqUtil.getDataQApi(url,params);
System.out.print(dataq.toString());
}


//*************调用该类直接跳过ssl证书 start **********************
//*************调用该类直接跳过ssl证书 start **********************
//*************调用该类直接跳过ssl证书 start **********************

//调用该类直接跳过ssl证书
static {
try {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier
(
(urlHostName, session) -> true
);
} catch (Exception e) {
}
}


/**
* 跳过ssl证书
*
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private static void trustAllHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[1];
trustAllCerts[0] = (TrustManager) new TrustAllManager();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

private static class TrustAllManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
}

//*************调用该类直接跳过ssl证书 end **********************
//*************调用该类直接跳过ssl证书 end **********************
//*************调用该类直接跳过ssl证书 end **********************




//*************dataq Api 调用的公共方法 start **********************
//*************dataq Api 调用的公共方法 start **********************
//*************dataq Api 调用的公共方法 start **********************

private static String getResult(String body, String method, HttpURLConnection conn) throws IOException {
PrintWriter out = new PrintWriter(conn.getOutputStream());
BufferedReader in = null;
String result = "";
try {
out.print(body);
out.flush();
System.out.println(conn.getResponseCode());
if (conn.getResponseCode() != 200) {
in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
} else {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
}
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(" " + method + " " + e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}

private static String generateSignature(String method, String ak_id, String ak_secret, String url, String body) throws MalformedURLException {
URL realUrl = new URL(url);
String accept = "json";
String content_type = "application/json";
String path = realUrl.getFile();
String date = toGMTString(new Date());
String bodyMd5 = MD5Base64(body);
String stringToSign = "";
if (method.equals("GET")) {
stringToSign = method + "\n" + accept + "\n" + content_type + "\n" + date + "\n" + path;
} else {
stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n" + path;
}
String signature = HMACSha1(stringToSign, ak_secret);
return "dtboost-proxy " + ak_id + ":" + signature;
}

public static String MD5Base64(String s) {
if (s == null)
return null;
String encodeStr = "";
byte[] utfBytes = s.getBytes();
MessageDigest mdTemp;
try {
mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(utfBytes);
byte[] md5Bytes = mdTemp.digest();
BASE64Encoder b64Encoder = new BASE64Encoder();
encodeStr = b64Encoder.encode(md5Bytes);
} catch (Exception e) {
throw new Error("Failed to generate MD5 : " + e.getMessage());
}
return encodeStr;
}

public static String HMACSha1(String data, String key) {
String result;
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
result = (new BASE64Encoder()).encode(rawHmac);
} catch (Exception e) {
throw new Error("Failed to generate HMAC : " + e.getMessage());
}
return result;
}

public static String toGMTString(Date date) {
SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
return df.format(date);
}

//*************dataq Api 调用的公共方法 end **********************
//*************dataq Api 调用的公共方法 end **********************
//*************dataq Api 调用的公共方法 end **********************

}

调用dataq

调用机制,失败重连机制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 请求,增加报错,重连机制,
* 默认请求3次,失败之后,停 10秒,然后在进行重试
* @param url
* @param params
* @return
*/
public Map<String,Object> dataqReqAgein2(String url,Map<String,String> params) throws InterruptedException {
Map<String,Object> mapResult= new HashMap<>();
Boolean reqAgein = false;
// 最大重连次数;
int ageinNumMax = 2;
int ageinNum = 0;
do{
ageinNum ++;

DataqUtil dataqUtil = new DataqUtil();
Map<String, Object> data = dataqUtil.getDataQApi(url,params);
String code = String.valueOf(data.get("code"));

//成功获取到数据之后,返回
if(code.equals("200")){
reqAgein = false;
mapResult = data;
break;
}else{
reqAgein = true;
}
//失败重连三次之后也返回
if(ageinNum > ageinNumMax){
reqAgein = false;
mapResult = data;
break;
}else{
Thread.sleep(1000*10);
}

}while (reqAgein);
return mapResult;
}

一辈子很短,努力的做好两件事就好;
第一件事是热爱生活,好好的去爱身边的人;
第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱;

继开 wechat
欢迎加我的微信,共同交流技术